package br.com.lh.urabuilder.view.properties
{
	import br.com.lh.urabuilder.domain.xml.UraActionField;
	import br.com.lh.urabuilder.event.SavePropertiesEvent;
	import br.com.lh.urabuilder.util.XMLUtils;
	import br.com.lh.urabuilder.view.ActionComponent;
	import br.com.lh.urabuilder.view.base.APopUpConfirmationBarContainer;
	
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.collections.XMLListCollection;
	import mx.containers.Form;
	import mx.containers.FormHeading;
	
	public class PropertiesForm extends APopUpConfirmationBarContainer{
		public var component:ActionComponent;
		protected var form:Form;
		protected var _validatorArray:Array = new Array();
		
		public function PropertiesForm(){
			super();
		} 
		
		public function updateValues():void{
			basicForm();
			createButtons();
			setFocus();
		}
		
		protected function basicForm():void{
			form = createForm();
			createFields(form);	
		}
		
		
		private function createFields(form:Form):void{
			var xml:XMLList = component.uraAction.getVariables();
			var coll:XMLListCollection  = new XMLListCollection(xml);
			XMLUtils.sortByName(coll);
			for each(var n:XML in coll){
			var nodeName:String =n.@name.toString();
			var nodeType:String = n.@type.toString();
			var uraActionField:UraActionField = component.uraAction[nodeName] as UraActionField;
			if (uraActionField!=null && uraActionField.validator != null){
				_validatorArray.push(uraActionField.validator);
			}
			if (nodeType.indexOf("UraActionField") != -1){
				form.addChild(new UraFormItem(nodeType,uraActionField));
				}
			}
		}
		
		private function createForm():Form{
			var form:Form = new Form();
			addChild(form);
			var formHeading:FormHeading = new FormHeading();
			formHeading.label =component.uraAction.xmlName();
			formHeading.setStyle("horizontalAllignment","left");
			form.addChild(formHeading);
			return form;
		}
				
		public override function okEvent():CairngormEvent{
			return new SavePropertiesEvent(component);
		}
		
		public override function get validatorArray():Array{
			return _validatorArray;
		}
		
		
		
		
	}
		
	
}